package www.notice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

import www.Action;
import www.ActionForward;
import www.notice.NoticeDTO;
import www.notice.NoticeMgr;
import www.utility.Utility;

public class NoticeCreate implements Action {
     @Override
    public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
         System.out.println("noticecreate executed");
//         http://211.241.228.81:9095/Android1_2/member/create.do
//         ?id=tt&introduce=ttintro&nickname=ttnick&profilephoto=ttphoto&password=tt&email=tt&memlevel=tt
         
         int noticeno = Integer.parseInt(request.getParameter("noticeno"));
         String nickname = request.getParameter("nickname");
         String title = request.getParameter("title");
         String contents = request.getParameter("contents");
         String profilephoto = request.getParameter("profilephoto");
         String noticedate = request.getParameter("noticedate");
         
         title = Utility.byteToString(title);
         contents = Utility.byteToString(contents);
         nickname = Utility.byteToString(nickname);
         profilephoto = Utility.byteToString(profilephoto);
         
         request.setCharacterEncoding("euc-kr");
         
         NoticeMgr mgr = new NoticeMgr();
         NoticeDTO dto = new NoticeDTO();
         
         dto.setNoticeno(noticeno);
         dto.setNickname(nickname);
         dto.setTitle(title);
         dto.setContents(contents);
         dto.setProfilephoto(profilephoto);
         dto.setNoticedate(noticedate);
         
         JSONObject obj = new JSONObject(); 
         
         try{
             int retval = (Integer) mgr.create(dto);
             System.out.println("retval: "+retval);
         if(retval == 1){
                 obj.put("code","success");
                 obj.put("message", "데이터 추가에  성공하였습니다.");
         }else{
               obj.put("retval",retval);
               obj.put("code","error");
               obj.put("message", "데이터 추가에 실패하였습니다.");
           }
         }catch(Exception e){
             System.out.println(e);
         }finally{
             
         }
             System.out.print("Notice: " +  obj);
             request.setAttribute("obj", obj);
            
             ActionForward forward = new ActionForward();
             forward.setRedirect(false);  // 
             forward.setPath("/obj.jsp");
             
             return forward;
     }
}
